package com.topView2;

import com.bean.ListNode;

import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * @Author: huangzhigao
 * @Date: 2023/2/12 13:52
 */
public class leecode23 {
    public ListNode mergeKLists(ListNode[] lists) {
        PriorityQueue<Bean> queue = new PriorityQueue<>(new Comparator<Bean>() {
            @Override
            public int compare(Bean o1, Bean o2) {
                return o1.val - o2.val;
            }
        });
        for (ListNode node : lists) {
            if (node != null) {
                queue.add(new Bean(node.val, node));
            }
        }

        ListNode dummy = new ListNode(-1);
        ListNode cur = dummy;
        while (!queue.isEmpty()) {
            Bean poll = queue.poll();
            cur.next = new ListNode(poll.val);
            cur = cur.next;
            ListNode next = poll.node.next;
            if (next != null) {
                queue.add(new Bean(next.val, next));
            }
        }
        return dummy.next;
    }

    class Bean {
        private int val;
        private ListNode node;

        public Bean(int val, ListNode node) {
            this.val = val;
            this.node = node;
        }
    }
}
